package com.prd.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.prd.config.LoginInterceptor;
import com.prd.dto.SysPostDTO;
import com.prd.model.SysPostDO;
import com.prd.mapper.SysPostMapper;
import com.prd.service.SysPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.util.CommonUtil;
import com.prd.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 部门岗位数据表 服务实现类
 * </p>
 *
 * @author lizhaoyi
 * @since 2024-01-04
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPostDO> implements SysPostService {

    @Autowired
    private SysPostMapper sysPostMapper;

    /**
     * 添加职务岗位
     * @param sysPostDTO
     * @return
     */
    @Override
    public JsonData add(SysPostDTO sysPostDTO) {

        SysPostDO sysPostDO = sysPostMapper.selectOne(new LambdaQueryWrapper<SysPostDO>().eq(SysPostDO::getDeptName, sysPostDTO.getDeptName()));

        if (sysPostDO != null) {
            return JsonData.buildError("部门岗位已存在。");
        }

        sysPostDTO.setCreateBy(LoginInterceptor.threadLocal.get().getName());
        sysPostDTO.setCreatedTime(LocalDateTime.now());

        sysPostMapper.insert(CommonUtil.dtoTransfer(sysPostDTO, SysPostDO.class));

        return JsonData.buildSuccess();
    }

    /**
     * 编辑
     * @param sysPostDTO
     * @return
     */
    @Override
    public JsonData edit(SysPostDTO sysPostDTO) {

        sysPostMapper.updateById(CommonUtil.dtoTransfer(sysPostDTO, SysPostDO.class));

        return JsonData.buildSuccess();
    }

    /**
     * 移除
     * @param ids
     * @return
     */
    @Override
    public JsonData deleteByIds(List<String> ids) {

        sysPostMapper.deleteBatchIds(ids);

        return JsonData.buildSuccess();
    }

    /**
     * 查询职务岗位列表
     * @return
     */
    @Override
    public JsonData selectList() {

        return JsonData.buildSuccess(sysPostMapper.selectList(new LambdaQueryWrapper<SysPostDO>().orderBy(false,false,SysPostDO::getCreatedTime)));
    }

    /**
     * 查询职务岗位
     * @param id
     * @return
     */
    @Override
    public JsonData selectOne(Long id) {
        return JsonData.buildSuccess(sysPostMapper.selectById(id));
    }
}
